package net.bwie.realtime.jtp.dim.function;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.RichFilterFunction;
import org.apache.flink.configuration.Configuration;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: FuHe
 * @Date: 2025/6/2
 */
public class DimDataFilterFunction extends RichFilterFunction<String> {
    private Set<String> dimSet;

    public DimDataFilterFunction(Set<String> dimSet) {
        this.dimSet = dimSet;
    }

    private final Set<String> operateTypeSet = new HashSet<String>() {
        {
            add("insert");
            add("update");
            add("delete");
        }
    };

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
    }

    @Override
    public boolean filter(String value) throws Exception {
        JSONObject jsonObject = JSON.parseObject(value);
        // 获取表名称
        String tableName = jsonObject.getString("table_name");
        // 操作类型
        String operateType = jsonObject.getString("operate_type");
        // 判断过滤
        return dimSet.contains(tableName) && operateTypeSet.contains(operateType);
    }

    @Override
    public void close() throws Exception {
        super.close();
    }
}
